package homework3;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class PreparationData {

    public void preparation(String path) {
        //1，构造File类型的对象与path文件关联
        File file = new File(path);
        String absolutePath = file.getAbsolutePath();
        //准备数据，创建多级目录和文件
        boolean mkdir = file.mkdir();
        System.out.println(mkdir ? "文件夹创建成功" : "文件夹创建失败");
        try {
            File file1 = null;
            //创建5个文件
            for (int i = 0; i < 50; i++) {
                //写入文件内容
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(absolutePath + "/file" + i + ".txt"));
                bufferedWriter.write("我的文件路径是：" + absolutePath + "/file" + i + ".txt");
                bufferedWriter.close();
            }
            //创建5个文件夹
            for (int i = 0; i < 50; i++) {
                file1 = new File(absolutePath + "/file" + i);
                boolean newFile = file1.mkdir();
                System.out.println(newFile ? "文件创建成功" : "文件创建失败");
                //分别创建5个子文件
                for (int j = 0; j < 20; j++) {
                    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(absolutePath + "/file" + i + "/sub" + j + ".txt"));
                    bufferedWriter.write("我的文件路径是：" + absolutePath + "/file" + i + "/sub" + j + ".txt");
                    bufferedWriter.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
